#include "ItemCacheData.h"



ItemCacheData::ItemCacheData()
	: m_maxCount(0)
{
}


ItemCacheData::~ItemCacheData()
{
	Clear();
}

bool ItemCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void ItemCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool ItemCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/item/item.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/item/item.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}


void ItemCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		Item* p = new Item;
		_safe_check(p);

		p->mId = jm["props_id"].asUInt();
		strcpy(p->mName, jm["name"].asString().c_str());
		p->mTypes = jm["types"].asUInt();
		strcpy(p->mDescribe, jm["describe"].asString().c_str());
		p->mGrade = jm["grade"].asUInt();
		p->mSprice = jm["sprice"].asUInt();
		p->mSkillPoint = jm["skill_point"].asUInt();
		p->mDropFb = jm["drop_fb"].asUInt();
		strcpy(p->mIconName, jm["icon_name"].asString().c_str());
		p->mReleased = jm["released"].asUInt();
		p->mPiles = jm["piles"].asUInt();

		// [0,0,0,0,0]
		Json::Value cprice = jm["cprice"];
		for (uint32_t i = 0; i < max_money_type; ++i)
		{
			p->mCPrice[i] = cprice[i].asUInt();
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

Item* ItemCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}

	_safe_guard_end;

	return nullptr;
}

uint32_t ItemCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}